package task3;

import java.util.Random;

public class Producer implements Runnable {

	private int _ID;
	private String myMessage;
	private CyclicBuffer<String> buffer;
	private static int STEPS_QUANTITY = 10;

    public Producer(int ID, CyclicBuffer<String> buffer, final String message) {
    	this._ID = ID;
        this.buffer = buffer;
        myMessage = new String();
        myMessage = myMessage.concat(message);
    }

    public void run() {
        Random random = new Random();
        boolean messageIsPut;

        for (int i = 0; i < STEPS_QUANTITY; i++) {
        	messageIsPut = buffer.put(myMessage + " STEP: " + i);
            if (!(messageIsPut)) {
            	System.out.println("PRODUCER " + _ID + " - BUFFER IS FULL. WAITING...");
            	i--;
            }
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) { }
        }        
    }
}